﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace PWEditor
{
    public partial class PWEditor : Form
    {
        private Grid _grid = new Grid(20, Color.White);
        private Map _map = null;

        private String _fileName;
        private String _filePath;

        //Flags for saving.
        private Boolean _isNeverSaved = false;
        private Boolean _isFileOpen = false;

        public PWEditor()
        {
            InitializeComponent();
            this.mainPanel.BackColor = Color.Black;
            _grid.Anchor = (AnchorStyles)((AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right));
            this.mainPanel.Controls.Add(_grid);
        }
 

        //Invalidate eventhandler
        protected override void OnInvalidated(InvalidateEventArgs e)
        {
            base.OnInvalidated(e);
            //If a file is opened, we'll have to enable the save options.
            if (_isFileOpen)
            {
                this.Text = _fileName + " - PWEditor";
                this.fileSave.Enabled = true;
                this.fileSaveAs.Enabled = true;
                this.fileClose.Enabled = true;
                this.mainPanel.Visible = true;
                Console.WriteLine(this.mainPanel.Width);
            }
            else
            {
                this.Text = "PWEditor";
                this.fileSave.Enabled = false;
                this.fileSaveAs.Enabled = false;
                this.fileClose.Enabled = false;
                this.mainPanel.Visible = false;
            }

        }

        private bool ActionConfirmed()
        {
            if (_isFileOpen)
            {
                //Show the confirmation dialog
                DialogResult r = MessageBox.Show("Do you want to save your changes", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                switch (r)
                {
                    case DialogResult.Yes:
                        //The user has chosen to save the file, so we save it and return true.
                        SaveFile();
                        return true;
                    case DialogResult.No:
                        //The user has chosen to discard the changes and wants to continue, so we return true. 
                        //We set our flag to false, so the OpenFile function won't ask for confirmation again.
                        _isFileOpen = false;
                        return true;
                    case DialogResult.Cancel:
                        //The user has chosen to cancel the current action, nothing will happen, so we return false.
                        return false;
                }
            }
            return false;
        }

        private void NewFile()
        {
            NewMap dialog = new NewMap();

            //Show the New map dialog
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //Check if a file is already open, if so ask the user to save changes.
                if (ActionConfirmed() || !_isFileOpen)
                {
                    _map = new Map(dialog.Rows, dialog.Cols);
                    _grid.Create(_map);

                    //A new file has been created.
                    _fileName = dialog.FileName;
                    _isFileOpen = true;
                    _isNeverSaved = true;
                }
            }
            this.Refresh();
        }

        private void OpenFile()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Map Files (*.xml)|*.xml";

            //Check if a file is already open, if so ask the user to save changes.
            if (ActionConfirmed() || !_isFileOpen)
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    _filePath = dialog.FileName;
                    _fileName = Path.GetFileNameWithoutExtension(_filePath);
                    _map = XMLInterpreter.LoadMap(_filePath);
                    _grid.Create(_map);
                    _isFileOpen = true;
                }
            }
            this.Refresh();
        }

        private void SaveFile()
        {
            if (_isFileOpen)
            {
                if (_isNeverSaved)
                {
                    //New files should always be "Saved As" because a filename has to be chosen.
                    SaveFileAs();
                }
                else
                {
                    //The file exists and can be saved.
                    XMLInterpreter.SaveMap(_map, _filePath);
                }
            }
            this.Refresh();
        }

        private void SaveFileAs()
        {
            if (_isFileOpen)
            {
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.Filter = "Map Files (*.xml)|*.xml";
                dialog.FileName = _fileName;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    _filePath = dialog.FileName;
                    _fileName = Path.GetFileNameWithoutExtension(_filePath);
                    _isNeverSaved = false;
                    SaveFile();
                }
            }
            this.Refresh();
        }

        private void CloseFile()
        {
            //Check if a file is already open, if so ask the user to save changes.
            if (ActionConfirmed())
            {
                _isNeverSaved = false;
                _isFileOpen = false;
                _map = null;
            }
            this.Refresh();
        }

        private void CloseApp()
        {
            //Check if a file is already open, if so ask the user to save changes.
            if (ActionConfirmed() || !_isFileOpen)
            {
                Close();
            }
        }


        //EventHandlers for menu items.

        //File menu
        private void fileNew_Click(object sender, EventArgs e) { NewFile(); }
        private void fileOpen_Click(object sender, EventArgs e) { OpenFile(); }
        private void fileSave_Click(object sender, EventArgs e) { SaveFile(); }
        private void fileSaveAs_Click(object sender, EventArgs e) { SaveFileAs(); }
        private void fileClose_Click(object sender, EventArgs e) { CloseFile(); }
        private void fileExit_Click(object sender, EventArgs e) { CloseApp(); }

        //Help menu
        private void helpAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("PWEditor\nVersion: "+Application.ProductVersion+"\nhttp://www.awesomestuff.nl", "About PWEditor", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        //EventHandler for key combo's.
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Control | Keys.N:
                    NewFile();
                    return true;
                case Keys.Control | Keys.O:
                    OpenFile();
                    return true;
                case Keys.Control | Keys.S:
                    SaveFile();
                    return true;
                case Keys.Control | Keys.Shift | Keys.S:
                    SaveFileAs();
                    return true;
                case Keys.Control | Keys.Q:
                    CloseFile();
                    return true;
                case Keys.Alt | Keys.F4:
                    CloseApp();
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);

        }
    }
}
